<?php
namespace Common\Model;

use Think\Model;

class TopMenuModel extends Model
{

    protected $tableName = 'top_menu';

    function get()
    {
        $key = 'manager_top_menu';
        $list = S($key);
        if ($list === false) {
            $map['is_hidden'] = 0;
            $list = $this->where($map)
                ->order('sort asc, id asc')
                ->select();
            S($key, $list, 86400);
        }      
        
        foreach ($list as &$m) {
            if (strpos($m['url'], 'http://') !== false || strpos($m['url'], 'https://') !== false) {
                $m['url'] = $m['url'];
            } elseif (strpos($m['url'], '://') !== false) {
                $m['url'] = addons_url($m['url']);
            } else {
                $m['url'] = U($m['url']);
            }

        }

        // 高亮样式处理 TODO
        $index = strtolower ( MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME );
        if(defined ( '_ADDONS' ))
            $index = strtolower ( _ADDONS . '/' . _CONTROLLER . '/' . _ACTION );          

        $has = false;

        foreach ($list as &$vo) {
            if(stripos($vo['url'], $index)!==false){
                $has = true;
                session('top_nav_index', $index);
                $vo['class'] = 'active';
            }else{
                $vo['class'] = '';
            }
        }

        $session_index = session('top_nav_index');
        if($has==false && !empty($session_index)){

            foreach ($list as &$vo) {
                if(stripos($vo['url'], $session_index)!==false){
                   $vo['class'] = 'active';
                }else{
                   $vo['class'] = '';
            }
            }            
        }

      
        
        return $list;
    }

    function clear()
    {
        $key = 'manager_top_menu';
        S($key, null);
    }
}
?>
